package com.jimikongjian.controller;

import com.jimikongjian.models.Notification;
import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.service.base.INotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by fuxiuyang on 17/3/16.
 */
@RestController("notificationController")
@RequestMapping("/v1")
public class NotificationController {

    @Autowired
    @Qualifier("notificationService")
    private INotificationService notificationService;


    /**
     * 创建一条消息
     * @param notification
     * @return
     */
    @RequestMapping(value = "/notification", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage createNotification(@RequestBody Notification notification){
        return notificationService.createNotification(notification);
    }

    /**
     * 查询通知消息
     * @param notifiedId
     * @param status
     * @param start
     * @param length
     * @return
     */
    @RequestMapping(value = "/notification", method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage selectNotification(@RequestParam(name = "notifiedId", defaultValue = "-1") Integer notifiedId,
                                              @RequestParam(name = "status", defaultValue = "-1") Integer status,
                                              @RequestParam(name = "start", defaultValue = "0") Integer start,
                                              @RequestParam(name = "length", defaultValue = "10") Integer length){
        if (status == Notification.STATUS_UNREAD){
            return notificationService.selectUnreadNotificationListByNotifiedId(notifiedId, start, length);
        }
        return notificationService.selectNotificationListByNotifiedId(notifiedId, start, length);
    }

    /**
     * 查询被通知者未读消息的条数
     * @param notifiedId
     * @return
     */
    @RequestMapping(value = "/unread_notification", method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage selectNotification(@RequestParam(name = "notifiedId", defaultValue = "-1") Integer notifiedId){
        return notificationService.selectUnreadCountByNotifiedId(notifiedId);
    }

    /**
     * 更新消息的状态
     * @param notification
     * @return
     */
    @RequestMapping(value = "/notification", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseMessage updateNotification(@RequestBody Notification notification){
        return notificationService.updateNotification(notification);
    }
}
